package me.aartikov.simplenavigationsample.slice;

import me.aartikov.alligator.NavigationContext;
import me.aartikov.alligator.NavigationContextBinder;
import me.aartikov.alligator.Navigator;
import me.aartikov.alligator.screenswitchers.FragmentScreenSwitcher;
import me.aartikov.simplenavigationsample.ResourceTable;
import me.aartikov.simplenavigationsample.SampleApplication;
import me.aartikov.simplenavigationsample.SampleTransitionAnimationProvider;
import me.aartikov.simplenavigationsample.screens.ScreenB;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;
import ohos.agp.window.service.WindowManager;

public class AbilityASlice extends AbilitySlice {
    private Navigator mNavigator = SampleApplication.getNavigator();
    private NavigationContextBinder mNavigationContextBinder = SampleApplication.getNavigationContextBinder();

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);
        super.setUIContent(ResourceTable.Layout_ability_a);
        ((Text) findComponentById(ResourceTable.Id_title)).setText(ResourceTable.String_screen_a);
        findComponentById(ResourceTable.Id_go_forward_to_b_button).setClickedListener(v -> mNavigator.goForward(new ScreenB()));
    }

    @Override
    public void onActive() {
        super.onActive();

        NavigationContext navigationContext = new NavigationContext.Builder(getAbility(), SampleApplication.getNavigationFactory())
                .transitionAnimationProvider(new SampleTransitionAnimationProvider())
                .screenSwitcher(new FragmentScreenSwitcher(SampleApplication.getNavigationFactory(), ((FractionAbility) getAbility()).getFractionManager()))
                .build();
        mNavigationContextBinder.bind(navigationContext);
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onStop() {
        super.onStop();
        mNavigationContextBinder.unbind(getAbility());
    }


    @Override
    public void onBackPressed() {
        if (!mNavigator.goBack()) {
            super.onBackPressed();
        }
    }
}
